package app.hello;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/hello")
public class HelloController {
    private final DateTimeFormatter formatter;

    public HelloController() {
        this.formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
    }

    /**
     * hello
     */
    @GetMapping()
    public Response hello() {
        String data;

        // 构建并返回data
        data = String.format("Hello! Current time is %s", formatter.format(LocalDateTime.now()));
        return new HelloResponse(200, "OK", data);
    }

    private interface Response {
        /**
         * get错误码
         *
         * @return 错误码
         */
        int getCode();

        /**
         * set错误码
         *
         * @param code 自定义错误码, 例如: 404, 200等
         */
        void setCode(int code);

        /**
         * get消息字符串
         *
         * @return message
         */
        String getMsg();

        /**
         * set消息字符串
         *
         * @param msg message
         */
        void setMsg(String msg);
    }

    static private class HelloResponse implements Response {
        private int code;
        private String msg;
        private final Object data;

        public Object getData() {
            return data;
        }

        @Override
        public int getCode() {
            return code;
        }

        @Override
        public void setCode(int code) {
            this.code = code;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        @Override
        public void setMsg(String msg) {
            this.msg = msg;
        }

        public HelloResponse(int code, String msg, Object data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }
}
